package com.yzt.message.custom;

import android.os.Parcel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yzt.zhuju.rongyun.IMMessageType;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;

import io.rong.common.ParcelUtils;
import io.rong.imlib.MessageTag;
import io.rong.imlib.model.MessageContent;

@MessageTag(value = IMMessageType.TIP_LONG_MSG ,flag = MessageTag.ISPERSISTED| MessageTag.ISCOUNTED)
public class RXLongTipMessageContent extends MessageContent {

    private String content;
    private String extra;


    public static RXLongTipMessageContent setCustomTipMessage(String content, String  extra){
        RXLongTipMessageContent mRXMessageContent =new RXLongTipMessageContent();
        mRXMessageContent.content =content;
        mRXMessageContent.extra = extra;
        return mRXMessageContent;
    }

    @Override
    public byte[] encode() {
        JSONObject object =new JSONObject();
        object.put("content",content);
        object.put("extra",extra);
        return object.toString().getBytes(StandardCharsets.UTF_8);

    }

    public RXLongTipMessageContent() {
    }

    public RXLongTipMessageContent(byte[] data) {
        super(data);
        String jsonStr=null;
        try {
            jsonStr =new String(data, StandardCharsets.UTF_8);
            JSONObject object = JSON.parseObject(jsonStr);

            setContent(object.getString("content"));
            setExtra(object.getString("extra"));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        ParcelUtils.writeToParcel(dest, content);
        ParcelUtils.writeToParcel(dest,extra);
    }

    public static final Creator<RXLongTipMessageContent> CREATOR = new Creator<RXLongTipMessageContent>() {

        @Override
        public RXLongTipMessageContent createFromParcel(Parcel source) {
            return new RXLongTipMessageContent(source);
        }

        @Override
        public RXLongTipMessageContent[] newArray(int size) {
            return new RXLongTipMessageContent[size];
        }
    };


    public RXLongTipMessageContent(Parcel parcel){

        content = ParcelUtils.readFromParcel(parcel);
        extra = ParcelUtils.readFromParcel(parcel);
    }

    @Override
    public int describeContents() {
        return 0;
    }


    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }


}
